:- module(process_templates, [main/1], [pillow, dcg]).

:- use_module(library(file_utils), [file_to_string/2]).

:- use_module(library(dec10_io), [
        tell/1, told/0,
        see/1, seen/0
                                 ]).

:- use_module(library(terms), [atom_concat/2]).

:- use_module(library(read), [read/1]).

:- use_module(library(system), [
        make_dirpath/1,
        file_property/2,
        file_exists/2,
        directory_files/2
                               ]).

:- use_module(portalfiles(get_page_info), [
        page_def/4,
        box_def/2
                             ]).
:- use_module(portalfiles(standard_page), [standard_page/5, standard_page_nb/3]).
:- use_module(portalfiles(web_utils), [generate_box/2]).
:- use_module(portalfiles(webdb_info), [no_bodyText/1]).


main([Type, InputDirectory, OutputDirectory]):-
        %% 'Type' is 'header', 'navigation', or 'all'
        test_accessible_dir(InputDirectory, 5),        %% Enter and list
        (
            test_accessible_dir(OutputDirectory, 2) -> %% Write permission
            true
        ;
            make_dirpath(OutputDirectory)              %% Create if inexistent
        ),
        process_pages(Type, InputDirectory, OutputDirectory).

main(Args):-
        throw(error_in_main(Args)).

process_pages(Type, InputDirectory, OutputDirectory):-
        test_files(InputDirectory, FileBases), % Test for undescribed files
%%        display(processing_files(FileBases)), nl,
        process_each_file(FileBases, Type, InputDirectory, OutputDirectory).

process_each_file([], _Type, _Input, _Output).
process_each_file([FileBase|Files], Type, InputDirectory, OutputDirectory):-
        (
            page_def(FileBase, Title, _URL, Structure) ->
            generate_box(Structure, SideBox),
            atom_concat(FileBase, '.html', HtmlFileName),
            atom_concat([InputDirectory, '/', HtmlFileName],  FileIn),
            atom_concat([OutputDirectory, '/', HtmlFileName], FileOut),
            file_to_string(FileIn, String),
            html2terms(String, StructuredPage),
            (
                extract_body(StructuredPage, Body) ->
                true
            ;
                display(error_extracting_body(FileBase)),
                nl,
                Body = []
            ),
	    (no_bodyText(FileBase) ->
	     standard_page_nb(Type, Title, HTML)
	    ;
             standard_page(Type, Title, SideBox, Body, HTML)
	    ),
            write_page(FileOut, HTML)
        ;
            display(not_found(FileBase)),
            nl
        ),
        process_each_file(Files, Type, InputDirectory, OutputDirectory).

        

test_files(Directory, Files):-
        directory_files(Directory, Candidates),
        filter(Candidates, Directory, Files).

filter([], _Directory, []).
filter([C|Cs], Directory, L):-
        atom_concat([Directory, '/', C], FullName),
        (
            file_property(FullName, type(regular)),
            atom_concat(B, '.html', C) ->
            L = [B|Fs]
        ;
            L = Fs
        ),
        filter(Cs, Directory, Fs).


write_page(FileOut, HTML):-
        tell(FileOut),
        output_html(HTML),
        told,
        !.
write_page(FileOut, _):- %% Something failed --- emergency close & exception
        told,
        throw(error_writing_HTML(FileOut)).

test_accessible_dir(Dir, Mode):-
        file_exists(Dir, Mode),
        file_property(Dir, type(directory)).

extract_body([env(body, _Options, Body)|_Rest], Body):- !.
extract_body([env(_Whatever, _Options, Inner)|_Rest], Body):-
        extract_body(Inner, Body).
extract_body([env(_Whatever, Inner)|_Rest], Body):-
        extract_body(Inner, Body).
extract_body([_|Rest], Body):-
        extract_body(Rest, Body).
